#!/bin/bash
set -e
set -u
echo "PRETEST: running $0 script"
echo "PRETEST: @TEST_INFO_TEXT@"
echo "PRETEST: executing @SUSAN_DIR_TO_TEST@ -?"
if ! @SUSAN_DIR_TO_TEST@ -? 2>&1 | grep Version >/dev/null; then
    echo "PRETEST:  ERROR: could not start director. Forgot to build before testing?"
    exit 1
fi

postgresql=@postgresql@
if [ -n "$postgresql" ]; then
  echo -n "PRETEST: checking postgresql connection ..."
  if ! psql postgres <<< "SELECT version()" 2>&1 >/dev/null; then
    echo "could not connect postgresql server"
    exit 1
  else
    echo "OK"
  fi
fi

mysql=@mysql@
if [ -n "$mysql" ]; then
  echo -n "PRETEST: checking mysql connection ..."
  if ! mysql <<< "SELECT version()" 2>&1 >/dev/null; then
    echo "could not connect mysql server"
    exit 1
  else
    echo "OK"
  fi
fi

nr_killed=0
for pidfile in $(find . -name susan-\*.pid); do
  ((nr_killed=nr_killed+1))
  kill -9 "$(cat "$pidfile")" >/dev/null 2>&1 || :
  rm -f "$pidfile"
done
if [ $nr_killed -gt 0 ]; then
  echo "PRETEST: killed $nr_killed dangling systemtest processes"
fi
